import Vue from 'vue';
import axios from 'axios';
import components from '@/components';
import App from './App';
import api from './api';
import tools from './utils/tools';
import router from './router';
import store from './store';
import filters from '@/utils/filters';
import packageInfo from '../package.json'; // packageInfo
import { Checkbox, Sticky, DropdownMenu, DropdownItem, ImagePreview, Swipe, SwipeItem, List, Uploader, Switch, Cell, CellGroup, RadioGroup, Radio, Icon, PullRefresh, Notify, PasswordInput, NumberKeyboard, Toast, Step, Steps, Button, Field, CheckboxGroup, Loading, Popup, Slider, Tab, Tabs, Picker, Row, Col, Skeleton, Dialog } from 'vant';
import 'vant/lib/index.css';
import 'lib-flexible/flexible';
// 全局引入index.css
import './assets/style/index.less'; // 过滤器
// import VConsole from 'vconsole';
// 复制到粘贴板插件
import VueClipboard from 'vue-clipboard2';
VueClipboard.config.autoSetContainer = true;
Vue.use(VueClipboard);
// var vConsole = new VConsole();
// console.log(vConsole);
// use
Vue.use(Checkbox)
  .use(CheckboxGroup)
  .use(Button)
  .use(Toast)
  .use(Popup)
  .use(Slider)
  .use(Tab)
  .use(Tabs)
  .use(Picker)
  .use(Row)
  .use(Col)
  .use(Loading)
  .use(Skeleton)
  .use(Dialog)
  .use(Field)
  .use(Steps)
  .use(Step)
  .use(Sticky)
  .use(PasswordInput)
  .use(NumberKeyboard)
  .use(PullRefresh)
  .use(Icon)
  .use(Radio)
  .use(RadioGroup)
  .use(Cell)
  .use(Switch)
  .use(Uploader)
  .use(ImagePreview)
  .use(CellGroup)
  .use(List)
  .use(Swipe)
  .use(SwipeItem)
  .use(DropdownMenu)
  .use(DropdownItem);
Vue.prototype.$API = api; ;
Vue.prototype.$tools = tools;
Vue.config.productionTip = false;
Vue.prototype.$SERVER_URL = process.env.VUE_APP_SERVER_URL;
// 注册EventBus事件对象
var EventBus = new Vue();
window.EventBus = EventBus;
Object.defineProperties(Vue.prototype, {
  $BUS: {
    get: function () {
      return EventBus;
    }
  }
});

// 注册全局组件
Object.keys(components).forEach(key => {
  const name = components[key].name;
  Vue.component(name, components[key]);
});
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));

// 版本检查
function checkVersion (to, from, next) {
  // 输出系统当前版本号
  console.info('%c系统当前版本号: ' + packageInfo.version, 'background:#000;color:yellow');
  axios.get(`../static/version.json?_=${Math.random()}`).then((response) => { // 访问前端服务器获取版本号
    if (response.status === 200 && packageInfo.version !== response.data.version) {
      Notify({
        message: '发现新版本，自动更新中...',
        type: 'danger'
      });
      console.info('%c新版本：: ' + response.data.version, 'background:#000;color:yellow');
      setTimeout(() => {
        location.reload(true);
      }, 1000);
      return;
    }
    ifVersionCorrect(to, from, next);
  }).catch((err) => {
    console.error('checkVersion', err);
    ifVersionCorrect(to, from, next);
  });
}
const whiteList = ['/login', '/register', '/forgotPassword', '/payPassword']; // 不重定向白名单

// 版本检查通过
function ifVersionCorrect (to, from, next) {
  let token = localStorage.getItem('token');
  // 判断是否是APP,isAPP
  if (to.query.isApp) {
    next();
    return;
  }
  // 判断是否是APP
  if (from.query.isApp) {
    let toQuery = JSON.parse(JSON.stringify(to.query));
    toQuery.isApp = from.query.isApp;
    next({
      path: to.path,
      query: toQuery
    });
    if (token) {
      console.log(!whiteList.includes(to.name, 0));
      if (!whiteList.includes(to.name, 0)) {
        next({
          path: to.path,
          query: toQuery
        });
      } else {
        next({
          path: '/home',
          query: toQuery
        });
      }
    } else {
      next();
    }
  } else {
    next();
  }
}
router.beforeEach((to, from, next) => {
  // 区分开发环境版本检测
  if (process.env.NODE_ENV === 'production') {
    checkVersion(to, from, next);
  } else {
    ifVersionCorrect(to, from, next);
  }
});
router.afterEach(() => {
  // NProgress.done(); // 结束
});

new Vue({
  data () {
    return {
      loaclLogs: [],
      menuWidth: 240,
      ws: null,
      contractWS: null
    };
  },
  router,
  store,
  render: h => h(App)
}).$mount('#app');
